package com.zhijiang.common.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.zhijiang.hospital.entrty.HospitalSmsDto;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: hesuan_test_zhijianghospital
 * @BelongsPackage: com.zhijiang.common.util
 * @Author: JunFu
 * @CreateTime: 2022-09-07  19:11
 * @Description: TODO
 * @Version: 1.0
 */
public class MassageUtil {
    public static void smsUtil(HospitalSmsDto hospitalSmsDto) {
        String urlStr = "http://192.168.1.110:8081/SmsServer.ashx";
        //创建json对象作为requestBody
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("phone", hospitalSmsDto.getPhone());
        jsonObject.put("content", hospitalSmsDto.getContent());
        System.out.println(JSONUtil.toJsonStr(jsonObject));
        // 添加请求头信息
        Map<String, String> heads = new HashMap<>();
        // 使用json发送请求，下面的是必须的
        heads.put("Content-Type", "application/json;charset=UTF-8");
        /**
         ** headerMap是添加的请求头，
         body是传入的参数，这里选择json，后端使用@RequestBody接收
         */
        HttpResponse response = HttpRequest.post(urlStr)
                .headerMap(heads, false)
                .body(String.valueOf("<request><id>1</id><phone>" + hospitalSmsDto.getPhone() + "</phone><content>" + hospitalSmsDto.getContent() + "</content></request>"))
                .timeout(5 * 60 * 1000)
                .execute();
    }

    public static void main(String[] args) {
        String urlStr = "http://192.168.1.110:8081/SmsServer.ashx";
        //创建json对象作为requestBody
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("phone","16607228157");
        jsonObject.put("content", "你好");
        System.out.println(JSONUtil.toJsonStr(jsonObject));
        // 添加请求头信息
        Map<String, String> heads = new HashMap<>();
        // 使用json发送请求，下面的是必须的
        heads.put("Content-Type", "application/json;charset=UTF-8");
        /**
         ** headerMap是添加的请求头，
         body是传入的参数，这里选择json，后端使用@RequestBody接收
         */
        HttpResponse response = HttpRequest.post(urlStr)
                .headerMap(heads, false)
                .body(String.valueOf("<request><id>1</id><phone>" +"16607228157" + "</phone><content>" + "你好"+ "</content></request>"))
                .timeout(5 * 60 * 1000)
                .execute();
    }
//   public static  void sendMsg(HospitalSmsDto hospitalSmsDto) {
//       CloseableHttpClient httpClient = HttpClients.createDefault();
//       HttpPost httpPost = new HttpPost("http://192.168.1.110:8081/SmsServer.ashx");
//       CloseableHttpResponse response = httpClient.execute(httpPost);
//       String s = EntityUtils.toString(response.getEntity(), "utf-8");
//       System.out.println(s);
//       response.close();
//       httpClient.close();
//
//   }


}